#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"comment.h"
#include "ui_music.h"
#include "customdelegete.h"

#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QIcon icon(":/3426e66706efb2b9689d58f8adc082f.png");
    QPixmap pixmap(":/3426e66706efb2b9689d58f8adc082f.png");

    setFixedSize(349,495);//获取界面的位置
    setWindowFlag(Qt::FramelessWindowHint);//去掉界面边框

    menuQuit = new QMenu(this);
    //设置菜单项文字颜色
    menuQuit->setStyleSheet("QMenu::item{color:white}");
    QAction *closeAct = new QAction(QIcon(":/关闭.png"), tr("退出"), this);
    menuQuit->addAction(closeAct);
    connect(menuQuit,&QMenu::triggered,this,[=]{//[=]：匿名函数捕获
        this->close();
    });

   // 创建客户端对象
    clientSocket = new QTcpSocket(this);

    // 尝试连接到服务器
    clientSocket->connectToHost("192.168.162.74", 9000);

    // 连接信号
    connect(clientSocket, &QTcpSocket::connected, this, &MainWindow::onConnected);
    //readyRead是有消息可读的信号
    connect(clientSocket, &QTcpSocket::readyRead, this, &MainWindow::onReadyRead);
    p=new menu;
    //断开有消息可读的信号
//    disconnect(clientSocket, &QTcpSocket::readyRead, this, &MainWindow::onReadyRead);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::RightButton){
        menuQuit->exec(QCursor::pos());//QCursor::pos()鼠标当前的位置
    }
    //鼠标当前位置 evebt->globalPos();
    //窗口当前位置 this->pos(); ,   窗口新位置:1.当鼠标按下时，获得橙色那段偏移值
    //2.当我鼠标拖动的时候，根据偏移值，算出新的窗口该有的位置
    if(event->button()==Qt::LeftButton){
        mOffset = event->globalPos()-this->pos();
    }
}
void MainWindow::paintEvent(QPaintEvent *event)
{
    QPixmap backgroundPixmap;
    backgroundPixmap.load(":/IMG_20240904_115124.jpg");
    QPainter painter(this);
    // 根据窗口大小调整图片尺寸，保持图片比例
    QPixmap scaledPixmap = backgroundPixmap.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    // 将调整后的图片设置为窗口背景
    painter.drawPixmap(this->rect(), scaledPixmap);
}
void MainWindow::Disconnect()
{
    disconnect(clientSocket, &QTcpSocket::readyRead, this, &MainWindow::onReadyRead);
}
void MainWindow::onConnected()
{
    qDebug()<<"连接成功 "<<clientSocket<<endl;
}
void MainWindow::onReadyRead()
{
    QMSG msg;
    clientSocket->read((char*)&msg,sizeof(msg));
//    QByteArray data = clientSocket->readAll();
//    memcpy(&msg,data.at(0),sizeof(msg));
    qDebug()<<"msg.type"<<msg.type<<endl;
    if(strcmp(msg.buf,"get1")==0)
    {
        qDebug()<<"注册成功 "<<endl;
    }
    if(strcmp(msg.buf,"get2")==0)
    {
        qDebug()<<"登录成功 "<<endl;
        this->hide();
        p->clientSocket = this->clientSocket;
        qDebug()<<"clientSocke11 "<<clientSocket<<endl;
        p->show();
        disconnect(clientSocket, &QTcpSocket::readyRead, this, &MainWindow::onReadyRead);
    }

//        if(msg.type==4)
//        {
//            qDebug()<<"接收了"<<i<<"次信号"<<endl;
//            song *s=(song*)msg.buf;

//            p->showlike(*s);
//            i++;
//        }
}
void MainWindow::on_pushButton_clicked()
{
    QMSG msg;
    msg.type=1;
    QString str=ui->lineEdit->text();//用户名
    std::string str1=str.toStdString();//将Qstring类型转为标准字符串才能获得char*
    QString stra=ui->lineEdit_2->text();//密码
    std::string str2=stra.toStdString();
    user s1(str1.c_str(),str2.c_str());//user的构造函数
    memcpy(msg.buf,&s1,sizeof(msg.buf));
    //连接信号
    clientSocket->write((char*)&msg,sizeof(msg));

//    //readyRead是有消息可读的信号
//    connect(clientSocket, &QTcpSocket::readyRead, this, &MainWindow::onReadyRead);
}

void MainWindow::on_pushButton_3_clicked()//登录
{
    QMSG msg;
    msg.type=2;
    QString str=ui->lineEdit->text();//用户名
    std::string str1=str.toStdString();//将Qstring类型转为标准字符串才能获得char*
    QString stra=ui->lineEdit_2->text();//密码
    std::string str2=stra.toStdString();
    user s1(str1.c_str(),str2.c_str());//user的构造函数
    memcpy(msg.buf,&s1,sizeof(msg.buf));
    //连接信号
    qDebug()<<msg.type<<endl;
    clientSocket->write((char*)&msg,sizeof(msg));
    qDebug()<<clientSocket<<endl;

}
